﻿@using EasyWeb.Caching
@model EWConfigurationDefaultCacheModel
@{
    ViewBag.Title = EWWebResources.Configuration_Cache_PageTitle;
}

@functions {
    string GetCacheType() {
        var modelCacheType = Type.GetType(Model.CacheType);
        if (modelCacheType == typeof(EWHttpApplicationCache))
        {
            return "app";
        }
        else if (modelCacheType == typeof(EWAppFabricCache))
        {
            return "appfabric";
        }
        else if (modelCacheType == typeof(EWMemcachedCache))
        {
            return "memcached";
        }
        return "custom";
    }
}

<fieldset class="easyweb-ewcache">
    <legend></legend>
    <div>
        <p>
            <input id="easyweb-cache-type-app-choose" name="easyweb-cache-type" type="radio" value="app" />
            <label for="easyweb-cache-type-app-choose">@EWWebResources.Configuration_Cache_HttpApplication</label>
        </p>
    </div>
    <div>
        <p>
            <input id="easyweb-cache-type-appfabric-choose" name="easyweb-cache-type" type="radio" value="appfabric" />
            <label for="easyweb-cache-type-appfabric-choose">@EWWebResources.Configuration_Cache_AppFabric</label>
        </p>
        <p>
            <label for="easyweb-cache-type-appfabric-name">@EWWebResources.Configuration_Cache_AppFabric_CacheName</label>
            <input id="easyweb-cache-type-appfabric-name" type="text" />
            <span>@EWWebResources.Configuration_Cache_AppFabric_CacheName_Notice</span>
        </p>
        <table id="easyweb-appfabric-table" class="ui-widget ui-helper-reset easyweb-ewsimple">
            <thead class="ui-helper-reset ui-widget-content">
                <tr>
                    <th style="width: 50%">@EWWebResources.Configuration_Cache_Server_Address</th>
                    <th style="width: 30%">@EWWebResources.Configuration_Cache_Server_Port</th>
                    <th style="width: 20%"></th>
                </tr>
            </thead>
            <tbody class="ui-helper-reset ui-widget-content"></tbody>
            <tfoot class="ui-helper-reset ui-widget-content">
                <tr>
                    <td colspan="3"><a href="javascript:void(0)" class="easyweb-ewsimple-link-button">@EWWebResources.Configuration_Cache_Server_Add</a></td>
                </tr>
            </tfoot>
        </table>
    </div>
    <div>
        <p>
            <input id="easyweb-cache-type-memcached-choose" name="easyweb-cache-type" type="radio" value="memcached" />
            <label for="easyweb-cache-type-memcached-choose">@EWWebResources.Configuration_Cache_Memcached</label>
        </p>
        <table id="easyweb-memcached-table" class="ui-widget ui-helper-reset easyweb-ewsimple">
            <thead class="ui-helper-reset ui-widget-content">
                <tr>
                    <th style="width: 50%">@EWWebResources.Configuration_Cache_Server_Address</th>
                    <th style="width: 30%">@EWWebResources.Configuration_Cache_Server_Port</th>
                    <th style="width: 20%"></th>
                </tr>
            </thead>
            <tbody class="ui-helper-reset ui-widget-content"></tbody>
            <tfoot class="ui-helper-reset ui-widget-content">
                <tr>
                    <td colspan="3"><a href="javascript:void(0)" class="easyweb-ewsimple-link-button">@EWWebResources.Configuration_Cache_Server_Add</a></td>
                </tr>
            </tfoot>
        </table>
    </div>
    <div>
        <p>
            <input id="easyweb-cache-type-custom-choose" name="easyweb-cache-type" type="radio" value="custom" />
            <label for="easyweb-cache-type-custom-choose">@EWWebResources.Configuration_Cache_Custom</label>
            <span>@EWWebResources.Configuration_Cache_Custom_Notice</span>
        </p>
        <input id="easyweb-cache-type-custom" type="text" />
        @Html.ValidationMessageFor(m => m.CacheType)
    </div>
    <div>
        <input id="easyweb-cache-type-app" type="hidden" value="@typeof(EWHttpApplicationCache).AssemblyQualifiedName" />
        <input id="easyweb-cache-type-memcached" type="hidden" value="@typeof(EWMemcachedCache).AssemblyQualifiedName" />
        <input id="easyweb-cache-type-appfabric" type="hidden" value="@typeof(EWAppFabricCache).AssemblyQualifiedName" />
        @using (Html.BeginRouteForm("EasyWeb_Configuration", FormMethod.Post, new { id = "easyweb-form-edit" }))
        {
            <input id="@Html.IdFor(m => m.CacheType)" name="@Html.NameFor(m => m.CacheType)" type="hidden" value="@Model.CacheType" />
            <input id="@Html.IdFor(m => m.Memcached)" name="@Html.NameFor(m => m.Memcached)" type="hidden" value="@EWMemcachedServerCollectionModelBinder.ToJson(Model.Memcached)" />
            <input id="@Html.IdFor(m => m.AppFabric.CacheName)" name="@Html.NameFor(m => m.AppFabric.CacheName)" type="hidden" value="@Model.AppFabric.CacheName" />
            <input id="@Html.IdFor(m => m.AppFabric.Servers)" name="@Html.NameFor(m => m.AppFabric.Servers)" type="hidden" value="@EWAppFabricServerCollectionModelBinder.ToJson(Model.AppFabric.Servers)" />
            @Html.HiddenFor(m => m.CacheTypeTimestamp)
            @Html.HiddenFor(m => m.MemcachedTimestamp)
            @Html.HiddenFor(m => m.AppFabricTimestamp)
            <input id="easyweb-action-submit" type="submit" value="@EWWebResources.Button_Save" />
            <a id="easyweb-action-cancel" href="@Url.RouteUrl("EasyWeb_Admin")">@EWWebResources.Button_Cancel</a>
        }
    </div>
</fieldset>

@section head {
    <style type="text/css">
        .easyweb-ewcache p {
            margin: .5em 0;
        }

            .easyweb-ewcache p input {
                vertical-align: middle;
            }

            .easyweb-ewcache p label, .easyweb-ewcache p span {
                margin: 0 0 0 .5em;
                vertical-align: middle;
            }

        #easyweb-cache-type-appfabric-name {
            width: 20em;
        }

        #easyweb-cache-type-custom {
            width: 50em;
        }

        .easyweb-ewcache div {
            margin: 2em 0;
        }
    </style>
}

@section scripts {
    <script type="text/javascript">
        (function ($) {
            $(function () {
                $("#easyweb-action-submit, #easyweb-action-cancel").button();

                EasyWeb.EWTemp.config = new EasyWeb.EWAdmin.EWDefaultCacheConfiguration(
                    "@Url.RouteUrl("EasyWeb_Configuration", new { action = "CacheServer" })",
                    "#@Html.IdFor(m => m.CacheType)",
                    "#@Html.IdFor(m => m.Memcached)",
                    "#@Html.IdFor(m => m.AppFabric.CacheName)",
                    "#@Html.IdFor(m => m.AppFabric.Servers)",
                    "@EWWebResources.Button_Edit",
                    "@EWWebResources.Button_Delete",
                    "@EWWebResources.Window_Title_Message",
                    "@EWWebResources.Button_Ok",
                    "@EWWebResources.Button_Cancel",
                    "@EWWebResources.Button_Close",
                    "@EWWebResources.Window_Text_DefaultError",
                    function () {
                        EasyWeb.EWTemp.config.addRecord(EasyWeb.EWTemp.config, this);
                    },
                    function () {
                        EasyWeb.EWTemp.config.editRecord(EasyWeb.EWTemp.config, this);
                    },
                    function () {
                        EasyWeb.EWTemp.config.deleteRecord(EasyWeb.EWTemp.config, this);
                    }
                );
                EasyWeb.EWTemp.config.initialize("@GetCacheType()", "@Model.CacheType");

                $("#easyweb-form-edit").submit(function () {
                    EasyWeb.EWTemp.config.refresh();
                });
            });
        })(jQuery);
    </script>
}